﻿var const_bottletype_normal = "Normal bottle";
var const_bottletype_secret = "Secret bottle";

var gain_type = 0;
var g_sendnew_imgsrc = "";

$(document).ready(function(){
	//init 数据
	getuserinfo();
	//init 接受bottle list的数据
	
	clear_bottlelist();
	
	get_bottlelist(0, 1);
	set_prevnext_flag(1);
	
	get_unread_num(); // unread瓶子数

	//init valid bottle numbers
	// 同步
	var data = $.ajax({ 
		  url: "php/interface/get_userdata.php", 
		  async: false 
		}).responseText; 
	get_userdata_callback(data);


	$('.btn-prev').hover(
		function(){
			$(this).removeClass();
			$(this).addClass('btn-prevhover sprite-btn');
		},
		function(){
			$(this).removeClass();
			$(this).addClass('btn-prev sprite-btn');
		});
		

	
	$('.btn-next').hover(
			function(){
				$(this).removeClass();
				$(this).addClass('btn-nexthover sprite-btn');
			},
			function(){
				$(this).removeClass();
				$(this).addClass('btn-next sprite-btn');
			});
	
	$('.ico_close_square').hover(
			function(){
				$(this).css('opacity',0.6);
				$(this).css('cursor','pointer');
			},
			function(){
				$(this).css('opacity',1);
			});
	
	$('#bottle_favour').hover(
			function(){
				$('#bottle_favour').css('cursor','pointer');
				$('#setting_head').removeClass();
				$('#setting_head').addClass('setting_headhover');
			},
			function(){
				$('#setting_head').removeClass();
				$('#setting_head').addClass('setting_head');
			});
	
	$('.toolbar_item').hover(
			function(){
				$(this).css('cursor','pointer');
				$(this).css('background-color','#FFEECA');
			},
			function(){
				$(this).css('background-color','');
			});
	
	$('#close_set_pannel').click(function(){
		$('#setting_pannel').hide();
	});
	$('#close_my_pannel').click(function(){
		my_bottle_hide();
	});
	
	$('#bottle_favour').click(
			function(){
				$('#setting_pannel').show();
				$('.bottle_pannel').hide();
				$('#select_bottle_pannel').hide();
				my_bottle_hide();
			});
	
	$('#closeimg').click(function(){
		$('.bottle_pannel').hide();
	});
	
	$('#discard_btn').click(function(){
		$('#bottle_panel').hide();
	});
	
	function textarea_keyup(){
		if($('#textarea_id').val().length != 0){
			$('#send_btn').removeClass('btn_disable');
		}else{
			$('#send_btn').addClass('btn_disable');
		}
	}
	
	// 键盘事件
	$('#textarea_id').keyup(textarea_keyup);
	
	// set发送消息的按钮的动作,设置为发送一个新的瓶子
	set_sendbtn_newbottle();
	
	
	// 点击设置按钮
	$('#other_setting').click(function(){
		$('#other_setting').css("background-position","0 -160px");
		$('#other_setting').css("color","#FFFFFF");
		$('#other_setting').css("cursor","default");
		
		$('#info_setting').css("cursor","pointer");
		$('#info_setting').css("color","#5F4521");
		$('#info_setting').css("background-position","0 -32px");
		
		$('#active_receive').show();
		$('#set_nickname').hide();
		$('#set_sex').hide();
		$('#set_area').hide();
	});
	
	$('#info_setting').click(function(){
		$('#info_setting').css("background-position","0 -64px");
		$('#info_setting').css("color","#FFFFFF");
		$('#info_setting').css("cursor","default");
		
		$('#other_setting').css("background-position","0 -96px");
		$('#other_setting').css("cursor","pointer");
		$('#other_setting').css("color","#5F4521");
		
		
		$('#active_receive').hide();
		$('#set_nickname').show();
		$('#set_sex').show();
		$('#set_area').show();
	});
	
	// set面包的按钮
	$('#setpannel_ok').click(function(){
		set_selfinfo_action();
	});
	$('#setpannel_cancel').click(function(){
		$('#setting_pannel').hide();
	});

	// bottle_list
	$('.bottlelist_dialogue').hover(
		function(){
			$(this).css('background-color','#EFEFEF');
		},
		function(){
			$(this).css('background-color','');
		}
	);
	
	
	$('#receive_bottles').click(function(){
		// 改变颜色
		$('#receive_bottles').css("background-position","0 -64px");
		$('#receive_bottles').css("color","#FFFFFF");
		$('#receive_bottles').css("cursor","default");
		
		$('#sended_bottles').css("background-position","0 -96px");
		$('#sended_bottles').css("cursor","pointer");
		$('#sended_bottles').css("color","#5F4521");
		
		clear_bottlelist();
		get_bottlelist(0, 1);
		set_prevnext_flag(1);
	});
	
	$('#sended_bottles').click(function(){
		// 改变颜色
		$('#sended_bottles').css("background-position","0 -160px");
		$('#sended_bottles').css("color","#FFFFFF");
		$('#sended_bottles').css("cursor","default");
		
		$('#receive_bottles').css("background-position","0 -32px");
		$('#receive_bottles').css("cursor","pointer");
		$('#receive_bottles').css("color","#5F4521");
		
		clear_bottlelist();
		get_bottlelist(0, 0);
		// set send flag
		set_prevnext_flag(0);
	});
	
	// bottlelist 翻页
	$('#btn-prev_bottlelist').click(function(){
		var pagestr = $('#bottle_list_page').html();
		var pos = pagestr.indexOf("/");
		if(pos != -1){
			var curpage = pagestr.substring(0,pos);
			var flag = $(this).attr("flag");
			get_bottlelist(parseInt(curpage)-1, flag);
		}
	});
	
	$('#btn-next_bottlelist').click(function(){
		var pagestr = $('#bottle_list_page').html();
		var pos = pagestr.indexOf("/");
		if(pos != -1){
			var curpage = pagestr.substring(0,pos);
			var flag = $(this).attr("flag");
			get_bottlelist(parseInt(curpage)+1, flag);
		}
	});
	
	// message 翻页
	$('#btn-prev_message').click(function(){
		var bid = $('#bottle_title').attr("bid");
		var page = $('#message_page').html();
		get_message_list(parseInt(page)-1,bid);
	});
	
	$('#btn-next_message').click(function(){
		var bid = $('#bottle_title').attr("bid");
		var page = $('#message_page').html();
		get_message_list(parseInt(page)+1,bid);
	});
	
	
	// 点击要扔的瓶子
	$('.select_box').click(function(){
		var btype = $(this).attr("btype");
		btype = parseInt(btype);
		send_bottle(btype);
	});
	
	$('#select_bottle_close').click(function(){
		$('#select_bottle_pannel').hide();
	});
	
	// 点击确认按钮操作
	$('#confirm_y').click(function(event){end_confirm('#isok_id');var bid = $('#isok_id').attr("bid");$.post('php/interface/delete_bottle.php',{bottle_id:bid}, delete_bottle_callback);event.stopPropagation();	});
	
	$('#confirm_n').click(function(){
		end_confirm('#isok_id');
	});
	
	$('#confirm_fish').click(function(){
		animat_over();
		end_confirm('#fish_id');
	});
	
	$('#confirm_open').click(function(){
		animat_over();
		end_confirm('#openbottle_id');
		$('#bottle_panel').show();
	});
	
	// 点击silent按钮操作

	
	// update userdata: lastlogin;
	update_lastlogin();

});

function animat_over(){
	$('#animators').hide();
	$('#bottle').css({
		  width:0,
		  height:0
		});
}


function get_message_list(vpage,bid){
	start_loading();
	$.post('php/interface/getbottle_by_id.php',{bottle_id:bid,page:vpage},get_bottle_callback);
}

function get_bottlelist(vpage, vtype){
	// 发送请求
	start_loading();
	$.post('php/interface/get_bottlelist.php', {page:vpage,type:vtype},get_bottlelist_callback);
}
// 获取瓶子列表callback
function get_bottlelist_callback(data){
	end_loading();
	gen_messagelist(data);
}



function set_prevnext_flag(flag){
	$('#btn-prev_bottlelist').attr("flag",flag);
	$('#btn-next_bottlelist').attr("flag",flag);
}


function get_bottle_animate(img_src){
	wrecking(this,img_src);
}
function get_bottle(){
	start_loading();
	$.post('php/interface/get_bottle.php', {},get_bottle_callback);
}

function pick_bottle(){
	start_loading();
	$.post('php/interface/get_bottle.php', {},pick_bottle_callback);
}
function pick_bottle_callback(data){
	end_loading();
	var ret = init_getbottle(data,1);
	
	if(ret == 0){
		var json = new Array();
		json = eval('(' + data + ')');
		var bottle_type	=  json.bottle_info.type;
		var img_src = get_bottle_imgsrc(bottle_type);

		get_bottle_animate(img_src);
	}
	// 没有找到瓶子，海藻代替
	if(ret == -2){
		var img_src = get_bottle_imgsrc(-1);
		
		get_bottle_animate(img_src);
	}
}

// 初始化获取到的返回的bottle数据后的对话框中的数据
// data: 服务器返回，flag: 区分是简单扔出还是，动画扔出
function init_getbottle(data,is_animate){
	var json = new Array();
	json = eval('(' + data + ')');
	
	var ret = parseInt(json.ret);
	if(ret < 0){
		if(ret == -1){
			//alert("no bottle picked");
			return -2;
		}
		if(ret == -2){
			alert("your pick is over today");
		}
		return -1;
	}
	
	// clear 
	clear_messagearea();
	clear_textarea();
	
	
	var page			=  json.page;
	
	var bottle_id		=  json.bottle_info.bottle_id;
	var bottle_type		=  json.bottle_info.type;
	var sender_nickname =  json.bottle_info.sender_info.nickname;
	var sender_location =  json.bottle_info.sender_info.location;
	var sender_head 	=  json.bottle_info.sender_info.head;
	var sender_sex 		=  json.bottle_info.sender_info.sex;
	
	var last_nickname 	=  json.bottle_info.last_response_info.nickname;
	var last_location 	=  json.bottle_info.last_response_info.location;
	var last_head 		=  json.bottle_info.last_response_info.head;
	var last_sex 		=  json.bottle_info.last_response_info.sex;
	
	for(var i=0; i<json.message_info.length; i++){
		var message = json.message_info[i].message;
		var time = json.message_info[i].time;
		var nickname = json.message_info[i].author_info.nickname;
		var location = json.message_info[i].author_info.location;
		var head = json.message_info[i].author_info.head;
		var sex = json.message_info[i].author_info.sex;
		// set html
		add_message(head, time,location,nickname,message );
	}
	
	var bottle_type_string = get_bottletype_string(bottle_type);
	bottle_init(const_bottletype_normal, 1, "reply",70, 1);
	$('#bottle_title').attr("bid", bottle_id);
	$('#message_page').html(page);
	
	if(is_animate==0){
		set_simple_sendbtn_replybottle();
	}
	else{
		set_sendbtn_replybottle();
	}
	
	
	return 0;
}
function get_bottle_callback(data){
	end_loading();
	if(data==-1) return;
	init_getbottle(data,0);
	$('#bottle_panel').show();
	$('#select_bottle_pannel').hide();
	$('#setting_pannel').hide();
}

function show_select_pannel(){
	var throw_n = $('#throw_p').html();
	if(throw_n == 0){
		alert("You have no bottles for today.");
		return;
	}
	$('#select_bottle_pannel').show();
	$('#setting_pannel').hide();
	$('.bottle_pannel').hide();
	my_bottle_hide();
}

function send_bottle(btype){
	clear_textarea();
	clear_messagearea();
	
	var type_str;
	switch(btype){
	case 0:
		type_str = const_bottletype_normal;
		break;
	case 1:
		type_str = const_bottletype_secret;
		break;
	default:
		type_str = const_bottletype_normal;
	}
	$('#bottle_title').attr("btype",btype);
	bottle_init(type_str, 0, "message",300, 0);
	set_sendbtn_newbottle();
	
	$('#bottle_panel').show();
	$('#select_bottle_pannel').hide();
	$('#setting_pannel').hide();
	//my_bottle_hide();
}
function my_bottles(){
	$('#my_bottles').show();
	$('#setting_pannel').hide();
	$('.bottle_pannel').hide();
	$('#select_bottle_pannel').hide();
	
}
function send_newbottle_action(){
	var sendmessage = $('#textarea_id').val();
	var bottle_type = get_bottletype();
	if(bottle_type==-1)
		return;
	var n_btype = parseInt(bottle_type);
	var img_src = "";
	$('a.select_box').each(function(){
		if($(this).attr('btype') == bottle_type){
			var url = $(this).find('span').css('background-image');
			img_src = url.substring(4,url.length-1);
			if(img_src[0]=='"'){
				img_src = img_src.substring(1,img_src.length-1);
			}
			//break;
		}
	});
	$('#bottle_panel').hide();
	g_sendnew_imgsrc = img_src;
	
	$.post('php/interface/throw_bottle.php',{message:sendmessage,type:bottle_type},throw_bottle_callback);
}


function send_replybottle_action(){
	var sendmessage = $('#textarea_id').val();
	var bottleid_str= $('#bottle_title').attr("bid");
	start_loading();
	$.post('php/interface/reply_bottle.php',{message:sendmessage,bottle_id:bottleid_str},replybottle_callback);
}

function send_simple_replybottle_action(){
	var sendmessage = $('#textarea_id').val();
	var bottleid_str= $('#bottle_title').attr("bid");
	start_loading();
	$.post('php/interface/reply_bottle.php',{message:sendmessage,bottle_id:bottleid_str},simply_replybottle_callback);
}

function set_selfinfo_action(){
	var nicknamestr = $('#nickname').val();
	var sexstr	= $('input:[name=sex]:radio:checked').val();
	var locationstr = $('#location').val();
	var active_receivestr = $("#checkreceive").attr("checked");
	if(active_receivestr==undefined){
		active_receivestr = 0;
	}
	if(active_receivestr=="checked"){
		active_receivestr=1;
	}
	
	$.post('php/interface/set_userinfo.php',
			{nickname:nicknamestr,sex:sexstr, location:locationstr,is_receive:active_receivestr},
			set_selfinfo_callback);
}

function throw_bottle_callback(data){
	if(data=="0"){
		$('#bottle_panel').hide();
		throwTestFn(g_sendnew_imgsrc);
		// 扔瓶子动作
	}
	else{
		$('#bottle_panel').hide();
		if(data==-1){
			alert("The user is not exist!");
		}
		if(data==-2){
			alert("Your bottle is expired for today.");
		}
		if(data==-3){
			alert("throw bottle failed");
		}
	}
}
function replybottle_callback(data){
	end_loading();
	$('#bottle_panel').hide();
	var img_src = get_bottle_imgsrc(data);
	throwTestFn(img_src);
}
function simply_replybottle_callback(data){
	end_loading();
	$('#bottle_panel').hide();
}
function set_selfinfo_callback(data){
	$('#setting_pannel').hide();
	getuserinfo();
}

function delete_bottle_callback(data){
	if(data != -1){
		$('.bottlelist_dialogue').each(function(){
			if($(this).attr("bid")==data){
				$(this).remove();
			}
		});
	}
}

function get_userdata_callback(data){
	if(data == -1) return;
	var json = new Array();
	
	json = eval('(' + data + ')');
	var vthrow_times = json.throw_times;
	var vpick_times = json.pick_times;
	var vcredits 	= json.credits;
	
	$('#throw_p').html(vthrow_times);
	$('#pick_p').html(vpick_times);
}



function add_message(head_path, time, fromplace, author_name,message){
	var html="<div class='infoContent'>";
	html	+="<div class='dialogue'>";
	html	+="<div class='avator'>";
	html	+="<img class='head' src='"+head_path+"'>";
	html	+="</div>";
	html	+="<div class='rightpart'>";
	html	+="<label  name='time' class='graytext'>"+time+"</label>";
	html	+="<br>";
	html	+="<label name='fromplace' class='graytext'>"+fromplace+"</label>";
	html	+="</div>";
	html	+="<p>"+ message +" </p>";
	html	+="<br>";
	html	+="<div class='author_name graytext'>"+author_name+"</div>";
	html	+="</div>";
	html	+="</div>";
	
	$(html).prependTo("#message_container");
	$("<div class='cutline'></div>").prependTo("#message_container");
}



//private 
function bottle_init(bottle_type, pageup_enable, sendframe_title,sendframe_height,
		messageframe_enable){
	$('#bottle_title').html(bottle_type);
	if(pageup_enable==0){
		$('#btn-prev').hide();
		$('#btn-next').hide();
	}
	else{
		$('#btn-prev').show();
		$('#btn-next').show();
	}
	
	$('#reply_title').html(sendframe_title);
	$('#textarea_id').css('height',sendframe_height+"px");
	if(messageframe_enable==0){
		$('#bottle_content').hide();
	}
	else{
		$('#bottle_content').show();
	}
}



//private---------------------------------------------------------
function get_bottletype(){
	var btype = $('#bottle_title').attr("btype");
	if(btype==""){return -1;}
	return btype;
}

//private
function get_bottletype_string(typeid){
	switch(typeid){
	case 0:
		return const_bottletype_normal;
		break;
	case 1:
		return const_bottletype_secret;
		break
	default:
		return "unknow type";
	}
}
function clear_textarea(){
	$('#textarea_id').val("");
}
function clear_messagearea(){
	var htmlstr = "<div class='cutline'></div>";
	htmlstr +="<div class='paper_bottom'><div class='paper_bottom_r'></div></div>";
	$('#message_container').html(htmlstr);
}
function set_sendbtn_newbottle(){
	$('#send_btn').unbind("click");
	$('#send_btn').click(function(){
		if($.trim($('#textarea_id').val()) == 0){
			return;
		}
		send_newbottle_action();
	});
}

function set_sendbtn_replybottle(){
	$('#send_btn').unbind("click");
	$('#send_btn').click(function(){
		if($.trim($('#textarea_id').val()) == 0){
			return;
		}
		send_replybottle_action();
	});
}

function set_simple_sendbtn_replybottle(){
	$('#send_btn').unbind("click");
	$('#send_btn').click(function(){
		if($.trim($('#textarea_id').val()) == 0){
			return;
		}
		send_simple_replybottle_action();
	});
}

function gen_messagelist(data){
	if(data==-1){
		return;
	}
	clear_bottlelist();

	var json = new Array();
	json = eval('(' + data + ')');
	
	var page = json.page;
	$('#bottle_list_page').html(page);
	
	for(var i=0; i<json.bottle_list.length; i++){
		var bottle_id	= json.bottle_list[i].bottle_id;
		var head_img 	= json.bottle_list[i].head_img;
		var date 		= json.bottle_list[i].message_date;
		var author 		= json.bottle_list[i].author;
		var message 	= json.bottle_list[i].message;
		var is_read		= json.bottle_list[i].is_read;
		var silent		= json.bottle_list[i].silent;
		
		// set html
		var html = add_messagelist(bottle_id, is_read, head_img, date,author,message,silent);

		$(html).appendTo("#messagelist");
	}
	
	// hover
	$('.bottlelist_dialogue').hover(
			function(){
				$(this).css('background-color','#EFEFEF');
			},
			function(){
				$(this).css('background-color','');
			}
		);
	
	// click
	$('.bottlelist_dialogue').click(function(){
		var bid = $(this).attr("bid");
		start_loading();
		$.post('php/interface/getbottle_by_id.php',{bottle_id:bid,page:0},get_bottle_callback);
		$(this).removeClass("bl_dia_unread");
	});
	
	//delet click
	$('.dialogue_del').click(function(event){
		start_confirm('#isok_id');
		var bid = $(this).attr("bid");
		$('#isok_id').attr("bid",bid);
		event.stopPropagation();
	});
	$('.dialogue_del').hover(
			function(){
				$(this).css('background-color','#0099FF');
			},
			function(){
				$(this).css('background-color','');
			});
			
	// 点击silent按钮操作
	$('.silent').click(function(event){
		var bid = $(this).attr("bid");
		var silent_ck = $(".silent").attr("checked");
		$.post('php/interface/set_silent.php',{bottle_id:bid,silent:silent_ck});	
		event.stopPropagation();
	});
	$('.a_silent').click(function(event){
		var bid = $(this).attr("bid");
		var silentid="#silentid"+bid;
		var silent_ck = $(silentid).attr("checked");
		if(silent_ck=="checked"){
			$(silentid).attr("checked",false);
		}else{
			$(silentid).attr("checked",true);
		}
		silent_ck = $(silentid).attr("checked");
		$.post('php/interface/set_silent.php',{bottle_id:bid,silent:silent_ck});
		event.stopPropagation();
	});
	
}

function add_messagelist(bottle_id, is_read, head_img, date, author_name, message,silent){
	var silent_str = "";
	var class_str = "bottlelist_dialogue";
	// unread
	if(parseInt(is_read)==0){
		class_str += ' bl_dia_unread';
	}
	if(silent==1){
		silent_str=" checked='true' ";
	}
	var html = "<div class='" + class_str + "' bid='"+bottle_id+"'>";
	html += "<div class='list_avator_div'>";
	html += "<img src='" + head_img + "' class='list_avator'>";
	html += "</div>";
	html += "<div class='rightpart'>";
	html += "<label class='graytext'>" + date + "</label><br>";
	html += "<a title='delete bottle' bid='"+bottle_id+"' class='dialogue_del'>delete</a>";
	html += "<br><a title='silent this bottle message'  bid='"+bottle_id+"' class='a_silent' >silent</a>";
	html += "<input id='silentid"+bottle_id+"'  type='checkbox' bid='"+bottle_id+"' class='silent' "+silent_str+"  />";
	html += "</div>";
	html += "<p><span >"+author_name+"</span><br>";
	html += "<span class='graytext'>"+message+"</span></p>";
	html += "</div>";
	return html;
}

function clear_bottlelist(){
	$('#messagelist').html("");
}

function start_loading(){
	$('#loading_id').show();
	popupTransparentMask();
}
function end_loading(){
	$('#loading_id').hide();
	hideTransparentMask();
}
function start_confirm(id){
	popupTransparentMask();
	$(id).show();
}

function end_confirm(id){
	hideTransparentMask();
	$(id).hide();
}




function getuserinfo(){
	$.post('php/interface/get_userinfo.php',{},get_userinfo_callback);
}
function get_userinfo_callback(data){
	if(data == -1) return;
	
	var json = new Array();
	json = eval('(' + data + ')');
	var nickname = json.nickname;
	var location = json.location;
	var head	 = json.head;
	var sex		 = json.sex;
	var receive_set = json.receive_setting;
	
	$('#nickname').attr("value",nickname);
	$('#location').attr("value",location);
	
	$('#user_head').attr("src",head);
	
	$('input[name=sex]').get(sex).checked = true;
	if(receive_set==1){
		$("#checkreceive").attr("checked",true);
	}else{
		$("#checkreceive").attr("checked",false);
	}
}

function get_bottle_imgsrc(bottle_type){
	var ibtype = parseInt(bottle_type);
	switch(ibtype){
	case 0:
		gain_type = 1;
		return "../images/pannel/normal_bottle.png";
	case 1:
		gain_type = 1;
		return "../images/pannel/secret_bottle.png";
	default:
		gain_type = 0;
		return "../images/pannel/fish.png";
	}
}

function update_lastlogin(){
	$.post('php/interface/update_lastlogin.php');
}

function get_unread_num(){
	$.post('php/interface/get_unread_num.php',{},get_unread_num_callback);
}
function get_unread_num_callback(data){
	if(data <= 0){
		$('#unread_p').html("");
		return;
	}
	$('#unread_p').html(data);
}


function my_bottle_hide(){
	$('#my_bottles').hide();
	get_unread_num(); // unread瓶子数
}

function popupMask() { 
	var windowWidth = $(document.body).width();
	var windowHeight = $(document.body).height();
	//添加并显示遮罩层 
	$("<div id='mask'></div>").addClass("mask") 
	.width(windowWidth) 
	.height(windowHeight) 
	.appendTo("body") 
	.show(); 
} 

function hideMask() { 
	$("#mask").remove(); 
}

function popupTransparentMask(){
	var windowWidth = $(document.body).width();
	var windowHeight = $(document.body).height();
	
	//添加并显示遮罩层 
	$("<div id='transparentmask'></div>").addClass("transparentmask") 
	.width(windowWidth) 
	.height(windowHeight) 
	.appendTo("body") 
	.show(); 
}
function hideTransparentMask() { 
	$("#transparentmask").remove(); 
}
